package cn.hy.mygame.entity;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import cn.hy.mygame.entity.EmplacePlant.PlantType;
import cn.hy.mygame.global.Config;
import cn.hy.mygame.model.BaseModel;
import cn.hy.mygame.model.TouchAble;
import cn.hy.mygame.view.GameView;

public class SeedPea extends BaseModel implements TouchAble{
	private int locationX;
	private int locationY;
	private boolean isAlive;
	private Rect touchArea;
	private long lastTouchTime = 0;
	
	public SeedPea (int locationX, int locationY) {
		this.locationX = locationX;
		this.locationY = locationY;
		touchArea = new Rect(locationX, locationY, locationX + Config.seedPea.getWidth(), locationY + Config.seedPea.getHeight());
		isAlive = true;
	}
	
	@Override
	public void drawSelf(Canvas canvas, Paint paint) {
		// TODO Auto-generated method stub
		canvas.drawBitmap(Config.seedPea, locationX, locationY, paint);
	}

	@Override
	public boolean onTouch(MotionEvent event) {
		// TODO Auto-generated method stub
		int x = (int) event.getX();
		int y = (int) event.getY();
		if ((System.currentTimeMillis() - lastTouchTime > 500)  && touchArea.contains(x, y)) {
			lastTouchTime = System.currentTimeMillis();
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				apply4EmplacePlant();
				break;
			case MotionEvent.ACTION_MOVE:
				
				break;
			case MotionEvent.ACTION_UP:
				
				break;
			default:
				break;
			}
			return true;
		}
		return false;
	}

	private void apply4EmplacePlant () {
		GameView.getInstance().apply4EmplacePlant(PlantType.PEA, locationX, locationY);
	}
	
	@Override
	public int getLocationX() {
		// TODO Auto-generated method stub
		return locationX;
	};
	
	@Override
	public int getLocationY() {
		// TODO Auto-generated method stub
		return locationY;
	}
	
	@Override
	public void setLocationX(int locationX) {
		this.locationX = locationX;
	};
	
	@Override
	public void setLocationY(int locationY) {
		this.locationY = locationY;
	}
	
	@Override
	public boolean isAlive() {
		return isAlive;
	}

	@Override
	public void setAlive(boolean isAlive) {
		this.isAlive = isAlive;
	}
}
